package com.ityy.ruiji.controller;

import com.ityy.ruiji.common.R;
import com.ityy.ruiji.entity.ShoppingCart;
import com.ityy.ruiji.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 展示的伪代码
     * @return
     */
    @GetMapping("/list")
    public R list(HttpSession session){

        Long userId = (Long) session.getAttribute("user");
        R result = shoppingCartService.listCartItemByUserId(userId);
        return result;
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpSession httpSession){
        Long user =(Long) httpSession.getAttribute("user");
        shoppingCart.setUserId(user);
        R result = shoppingCartService.add(shoppingCart);
        return result;
    }

    /**
     * 作用：清空购物车
     * url: http://localhost:8080/shoppingCart/clean
     * method: delete
     * 返回值：R code
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        R result = shoppingCartService.cleanShoppingCart(userId);
        return result;
    }

}
